<?php

namespace App\Http\Controllers\Weixin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

use App\Repositories\Eloquent\Mgt\UtilRepository as Util;
use App\Repositories\Eloquent\Act\TopicRepository as Topic;
use App\Repositories\Eloquent\Act\QuestionRepository as Question;
use App\Repositories\Eloquent\Act\JoinRepository as Join;

class ActController extends Controller
{

    protected $topic, $question, $util;

    public function __construct(Topic $topic, Question $question, Util $util)
    {
        parent::__construct();

        $this->topic = $topic;

        $this->question = $question;

        $this->util = $util;

    }

    public function topic($uuid)
    {
        $topic = $this->topic->byUuid($uuid);

        if ($topic instanceof JsonResponse && $topic->getStatusCode() === 404) {
            return abort('404');
        }

        $qNumber = $topic->questions()->where('status', STATUS_NORMAL)->count();

        $questions = $topic->questions()->where('status', STATUS_NORMAL)->simplePaginate(1);
        
        return view($this->view, compact('topic', 'questions','qNumber'));
    }


    public function answer(Request $request)
    {
        return $this->topic->answer($request);
    }

    public function sms(Request $request)
    {
        return $this->util->sendSms($request);
    }

}